const nodemailer = require('nodemailer');


// 邮箱账户配置
const emailConfigs = [
    {
        service: 'Gmail',
        auth: {
            user: 'xuxiaowen89@gmail.com',
            pass: '8750196@gg'
        }
    },
    {
        service: 'Outlook',
        auth: {
            user: 'Jeking.Xu@outlook.com',
            pass: '8750196@Wr'
        }
    }
    ,
    {
        service: 'QQ',
        auth: {
            user: 'xuxiaowenfox@foxmail.com',
            pass: 'pdgpsmhkpscscaaj'
        }
    }
    // 添加更多的邮箱账户配置
];

//定义一个循环发送邮件账户的序号
var AD=0;

// 创建一个循环队列来轮流使用不同的邮箱账户
class CircularQueue {
    constructor(data,AD) {
        this.data = data;
        this.index = 0;
    }

    next() {
        // if (this.index === this.data.length - 1) {
        //     this.index = 0;
           
        // } else {
        //     this.index++;
        // }
        console.log( this.index,this.data.length,AD)
        console.log(this.data[this.index])
        // return this.data[this.index];
        return this.data[AD];
    }
}

// 创建循环队列实例
const emailQueue = new CircularQueue(emailConfigs,AD);

// 发送邮件函数
async function sendEmail(to, subject, text) {
    const emailConfig = emailQueue.next();
    
    // 创建 Nodemailer transporter
    let transporter = nodemailer.createTransport(emailConfig);

    // 设置邮件选项
    let mailOptions = {
        from: emailConfig.auth.user,
        to: to,
        subject: subject,
        text: text
    };

    // 发送邮件
    let info = await transporter.sendMail(mailOptions);
    console.log('Message sent: %s', info.messageId);
}

// 示例用法
async function main() {
    try {
        // 发送邮件
        AD=1;
        await sendEmail('531556397@qq.com', 'Test2发送邮件给我测试 Subject', 'This is a jeking email.');
        AD=2;
        await sendEmail('531556397@qq.com', 'Test3发送邮件给我测试 Subject', 'This is a jeking email.');

        console.log('Email sent successfully.');
    } catch (error) {
        console.error('Error sending email:', error);
    }
}

main();
